CheckBox = {}
CheckBoxFunc = {}
CheckBoxFunc.__index = CheckBoxFunc
setmetatable(CheckBox, CheckBoxFunc)

CheckBoxFunc.create = function(self, menu, action, target)
  local l = {}
  setmetatable(l, CheckBoxFunc)
  l.menu = menu
  l.action = action
  l.target = target
  l.selectState = {}
  l:setMenuItemExtTable()
  return l
end

CheckBoxFunc.setMenuItemExtTable = function(self)
  local items = self.menu:getChildren()
  for i=0,items:count()-1 do
    local item = items:objectAtIndex(i)
    setObjectExtTable(item, self)
    self.selectState[i+1] = -1
  end
end

CheckBoxFunc.getMenuItemIndex = function(self, node)
  local items = self.menu:getChildren()
  for i=0,items:count()-1 do
    local item = items:objectAtIndex(i)
    item = TypeCast(item, "CCMenuItem")
    if item==node then
      return i+1
    end
  end
end

CheckBoxFunc.setMenuState = function(self, index)
  local items = self.menu:getChildren()
  for i=0,items:count()-1 do
    local item = TypeCast(items:objectAtIndex(i), "CCMenuItem")
    if self.selectState[i+1]>0 then
      item:selected()
    else
      item:unselected()
    end
  end 
end

CheckBoxFunc.onSelectMenuItem = function(self, node)
  local index=self:getMenuItemIndex(node)
  self.selectState[index] = -self.selectState[index]
  self:setMenuState(index)
end
